package cool.taomu.toolkit.core.classloader

import com.google.common.eventbus.EventBus
import java.net.URL
import java.util.concurrent.ConcurrentHashMap
import org.eclipse.xtend.lib.annotations.Accessors
import org.slf4j.LoggerFactory

interface ITaomuPlugin {
    def void start()

    def void stop()
}

@Accessors
@Deprecated
class TaomuPluginManager implements AutoCloseable {
    var static LOG = LoggerFactory.getLogger(TaomuPluginManager);
    protected var static loaderMap = new ConcurrentHashMap<String, TaomuPluginClassloader>();
    protected var String packageName = "";
    protected var EventBus bus;

    new() {
    }

    def loader(String name) {
        var cl = loaderMap.values.findFirst [
            it.loadClass(name) !== null;
        ]
        return cl.loadClass(name)
    }

    def getPluginSet() {
        return loaderMap.keySet;
    }

    private def addLoader(String name, TaomuPluginClassloader cl) {
        loaderMap.put(name, cl);
    }

    def load(String name, Class<? extends ITaomuPlugin> zlass) {
        var loader = new TaomuPluginClassloader();
        loader.loadFile(zlass);
        addLoader(name, loader);
    }

    @SuppressWarnings(value="all")
    def load(URL url) {
        val loader = new TaomuPluginClassloader();
        loader.loadFile(url);
    }

    @SuppressWarnings(value="all")
    def load(URL url, EventBus bus) {
        val loader = new TaomuPluginClassloader();
        loader.loadFile(url);
    // Thread.currentThread.contextClassLoader = tccl;
    }

    def load(String name, URL url) {
        var loader = new TaomuPluginClassloader();
        loader.loadFile(url);
    }

    private def load(String name, TaomuPluginClassloader loader) {
        loaderMap.remove(name);
        addLoader(name, loader);
    }

    def unload(String name) {
    }

    def unloadAll() {
        pluginSet.forEach [
            unload(it);
        ]
    }

    override close() throws Exception {
        throw new UnsupportedOperationException("TODO: auto-generated method stub")
    }

}
